"""
Case Type   : 事件触发器
Case Name   : 【事件触发器】CREATE/ALTER/DROP TABLE命令触发事件触发器
Create At   :2024/07/08
Owner       :sungang14
Description :
    预置条件：
        1.创建事件触发器：
       CREATE OR REPLACE FUNCTION f_eventtrigger_0018()
       RETURNS event_trigger
       LANGUAGE plpgsql
       AS $$ BEGIN
       RAISE NOTICE 'test_event_trigger: % %', tg_event, tg_tag;
       END;
       $$;
       CREATE EVENT TRIGGER start_eventtri ON ddl_command_start EXECUTE PROCEDURE f_eventtrigger_0018();
       CREATE EVENT TRIGGER end_eventtri ON ddl_command_end EXECUTE PROCEDURE f_eventtrigger_0018();
       CREATE EVENT TRIGGER drop_eventtri ON sql_drop EXECUTE PROCEDURE f_eventtrigger_0018();
       CREATE EVENT TRIGGER rewrite_eventtri ON table_rewrite EXECUTE PROCEDURE f_eventtrigger_0018(); 
    测试步骤：
        1、CREATE TABLE t_eventtrigger_0018(id serial primary key, foo float);
        2、insert into t_eventtrigger_0018 select x * 1.001 from generate_series(1,500) as t(x);
        3、ALTER TABLE t_eventtrigger_0018 alter column foo type numeric;
        4、ALTER TABLE t_eventtrigger_0018 drop column foo;
        5、DROP TABLE t_eventtrigger_0018;
Expect      : 
    1、触发执行函数f_eventtrigger_0018()
        test_event_trigger: ddl_command_start,CREATE TABLE
        test_event_trigger: ddl_command_end,CREATE TABLE
    2、不触发
    3、触发执行函数f_eventtrigger_0018()
        test_event_trigger: ddl_command_start,ALTER TABLE
        test_event_trigger: table_rewrite,ALTER TABLE
        test_event_trigger: ddl_command_end,ALTER TABLE
    4、触发执行函数f_eventtrigger_0018()
        test_event_trigger: ddl_command_start,ALTER TABLE
        test_event_trigger: sql_drop,ALTER TABLE
        test_event_trigger: ddl_command_end,ALTER TABLE
    5、触发执行函数f_eventtrigger_0018()
        test_event_trigger: ddl_command_start,DROP TABLE
        test_event_trigger: sql_drop,DROP TABLE
        test_event_trigger: ddl_command_end,DROP TABLE
History     : 
"""

import sys
import os
import unittest
from yat.test import Node
from yat.test import macro

sys.path.append(sys.path[0] + "/../")
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH


class IndexFileDamaged(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f'-----{os.path.basename(__file__)} start-----')
        self.logger.info('----------------this is setup-----------------------')
        self.DB_ENV_PATH = macro.DB_ENV_PATH
        self.DB_INSTANCE_PATH = macro.DB_INSTANCE_PATH
        self.com = Common()
        self.comsh = CommonSH('PrimaryDbBmode')
        self.userNode = self.comsh.node
        self.comsh.execut_db_sql("drop EVENT TRIGGER if exists start_eventtri;")
        self.comsh.execut_db_sql("drop EVENT TRIGGER if exists end_eventtri;")
        self.comsh.execut_db_sql("drop EVENT TRIGGER if exists drop_eventtri;")
        self.comsh.execut_db_sql("drop EVENT TRIGGER if exists rewrite_eventtri;")
        msg = self.comsh.execut_db_sql("drop function if exists f_eventtrigger_0018();")
        msg = self.comsh.execut_db_sql(
            f"CREATE OR REPLACE FUNCTION f_eventtrigger_0018() RETURNS event_trigger LANGUAGE plpgsql" \
            f" AS \$\$ BEGIN" \
            f" RAISE NOTICE 'test_event_trigger: % %', tg_event, tg_tag;" \
            f"END;" \
            f"\$\$;")
        self.logger.info(msg)
        self.assertEqual("CREATE FUNCTION", msg)
        msg = self.comsh.execut_db_sql(
            "CREATE EVENT TRIGGER start_eventtri ON ddl_command_start EXECUTE PROCEDURE f_eventtrigger_0018();")
        self.logger.info(msg)
        self.assertEqual("CREATE EVENT TRIGGER", msg)
        msg = self.comsh.execut_db_sql(
            "CREATE EVENT TRIGGER end_eventtri ON ddl_command_end EXECUTE PROCEDURE f_eventtrigger_0018();")
        self.logger.info(msg)
        self.assertEqual("CREATE EVENT TRIGGER", msg)
        msg = self.comsh.execut_db_sql(
            "CREATE EVENT TRIGGER drop_eventtri ON sql_drop EXECUTE PROCEDURE f_eventtrigger_0018();")
        self.logger.info(msg)
        self.assertEqual("CREATE EVENT TRIGGER", msg)
        msg = self.comsh.execut_db_sql(
            "CREATE EVENT TRIGGER rewrite_eventtri ON table_rewrite EXECUTE PROCEDURE f_eventtrigger_0018();")
        self.logger.info(msg)
        self.assertEqual("CREATE EVENT TRIGGER", msg)
        self.comsh.execut_db_sql("drop table if exists t_eventtrigger_0018;")

    def test_Index_file_damaged(self):
        self.logger.info('创建表CREATE TABLE t_eventtrigger_0018(id serial primary key, foo float);')
        msg = self.comsh.execut_db_sql('CREATE TABLE t_eventtrigger_0018(id serial primary key, foo float);')
        self.logger.info(msg)
        self.assertIn(f'CREATE TABLE', msg)
        self.assertIn(f'test_event_trigger: ddl_command_start CREATE TABLE', msg)
        self.assertIn(f'test_event_trigger: ddl_command_end CREATE TABLE', msg)

        self.logger.info(
            '插入表数据insert into t_eventtrigger_0018 select x * 1.001 from generate_series(1,500) as t(x);')
        msg = self.comsh.execut_db_sql(
            'insert into t_eventtrigger_0018 select x * 1.001 from generate_series(1,500) as t(x);')
        self.logger.info(msg)
        self.assertEqual("INSERT 0 500", msg)

        self.logger.info('修改表结构ALTER TABLE t_eventtrigger_0018 alter column foo type numeric;')
        msg = self.comsh.execut_db_sql('ALTER TABLE t_eventtrigger_0018 alter column foo type numeric;')
        self.logger.info(msg)
        self.assertIn(f'test_event_trigger: ddl_command_start ALTER TABLE', msg)
        self.assertIn(f'test_event_trigger: table_rewrite ALTER TABLE', msg)
        self.assertIn(f'test_event_trigger: ddl_command_end ALTER TABLE', msg)
        self.assertIn("ALTER TABLE", msg)

        self.logger.info('修改表结构ALTER TABLE t_eventtrigger_0018 drop column foo;')
        msg = self.comsh.execut_db_sql('ALTER TABLE t_eventtrigger_0018 drop column foo;')
        self.logger.info(msg)
        self.assertIn(f'test_event_trigger: ddl_command_start ALTER TABLE', msg)
        self.assertIn(f'test_event_trigger: sql_drop ALTER TABLE', msg)
        self.assertIn(f'test_event_trigger: ddl_command_end ALTER TABLE', msg)
        self.assertIn("ALTER TABLE", msg)

        self.logger.info('删除表DROP TABLE t_eventtrigger_0018;')
        msg = self.comsh.execut_db_sql('DROP TABLE t_eventtrigger_0018;')
        self.logger.info(msg)
        self.assertIn(f'test_event_trigger: ddl_command_start DROP TABLE', msg)
        self.assertIn(f'test_event_trigger: sql_drop DROP TABLE', msg)
        self.assertIn(f'test_event_trigger: ddl_command_end DROP TABLE', msg)
        self.assertIn("DROP TABLE", msg)

    def tearDown(self):
        self.logger.info('----------------this is tearDown-----------------------')
        # 删除事件触发器
        msg = self.comsh.execut_db_sql("drop EVENT TRIGGER if exists start_eventtri;")
        self.logger.info(msg)
        self.assertEqual('DROP EVENT TRIGGER', msg)
        msg = self.comsh.execut_db_sql("drop EVENT TRIGGER if exists end_eventtri;")
        self.logger.info(msg)
        self.assertEqual('DROP EVENT TRIGGER', msg)
        msg = self.comsh.execut_db_sql("drop EVENT TRIGGER if exists drop_eventtri;")
        self.logger.info(msg)
        self.assertEqual('DROP EVENT TRIGGER', msg)
        msg = self.comsh.execut_db_sql("drop EVENT TRIGGER if exists rewrite_eventtri;")
        self.logger.info(msg)
        self.assertEqual('DROP EVENT TRIGGER', msg)
        # 删除事件触发器函数
        msg = self.comsh.execut_db_sql("drop function if exists f_eventtrigger_0018();")
        self.logger.info(msg)
        self.assertEqual('DROP FUNCTION', msg)
        # 删除表
        msg = self.comsh.execut_db_sql("drop table if exists t_eventtrigger_0018;")
        self.logger.info(msg)
        self.assertIn('DROP TABLE', msg)
        self.logger.info(f'-----{os.path.basename(__file__)} end-----')
